'''
Author: 红星大哥 zhxlx@foxmail.com
Date: 2024-07-10 18:04:51
LastEditors: home zhxlx@foxmail.com
LastEditTime: 2024-07-10 22:02:37
FilePath: \esp8266\app_sd.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
# sd file system

'''
help(SDCard)
object <class 'SDCard'> is of type type
  info -- <function>
  __del__ -- <function>
  deinit -- <function>
  readblocks -- <function>
  writeblocks -- <function>
  ioctl -- <function>
'''

'''
os.listdir([dir])¶
With no argument, list the current directory. Otherwise list the given directory.

os.mkdir(path)¶
Create a new directory.

os.chdir(path)¶
Change current directory.

os.getcwd()¶
Get the current directory.

os.ilistdir([dir])¶

os.remove(path)¶
Remove a file.

os.rmdir(path)¶
Remove a directory.

os.rename(old_path, new_path)¶
Rename a file.

os.stat(path)¶
Get the status of a file or directory.

os.statvfs(path)¶
Get the status of a filesystem.
'''
import os
#----------------------------------------------------
# 打开一个文件用于写入（如果不存在则创建），创建和写入文件
with open('example.txt', 'w') as f:
    # 写入文本
    f.write("Hello, MicroPython!\n")
    # 写入更多文本
    f.write("This is a test.\n")

# 如果你想写入二进制数据，可以使用 'wb' 模式
with open('example.bin', 'wb') as f:
    f.write(b'\x00\x01\x02\x03')
    
#----------------------------------------------------
# 打开一个文件用于读取，读取文件
with open('example.txt', 'r') as f:
    # 读取整个文件内容
    content = f.read()
    print(content)

# 或者逐行读取
with open('example.txt', 'r') as f:
    for line in f:
        print(line.strip())  # strip() 移除行尾的换行符

# 读取二进制数据
with open('example.bin', 'rb') as f:
    binary_data = f.read()
    print(binary_data)
    
#----------------------------------------------------
# 打开一个文件用于追加，追加到文件
with open('example.txt', 'a') as f:
    f.write("Appending some text.\n")
    
#----------------------------------------------------
# 打开文件，文件位置操作
f = open('example.txt', 'r+')
# 将文件指针移动到文件开头
f.seek(0)
# 读取前几个字符
print(f.read(5))
# 将文件指针移动到文件末尾
f.seek(0, 2)
# 在文件末尾写入
f.write("End of file.")
# 不要忘记关闭文件
f.close()
#----------------------------------------------------
#判断文件是否存在

# 获取当前工作目录
current_dir = os.getcwd()
print("Current directory:", current_dir)

# 改变工作目录
os.chdir('/path/to/new/directory')

# 再次获取当前工作目录以确认已更改
new_dir = os.getcwd()
print("New directory:", new_dir)

# 文件路径
file_path = '/path/to/file.txt'

try:
    os.stat(file_path)
    print("File exists.")
except OSError:
    print("File does not exist.")
#----------------------------------------------------
import utime
# 获取当前时间元组
current_time = utime.localtime()

# 定义日期时间格式
format_string = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}"

# 格式化日期时间
formatted_time = format_string.format(*current_time)

# 输出结果
print(formatted_time)
#----------------------------------------------------

def time_dir() -> str:
  return ''
